import { createModel } from 'hox';
import { useState } from 'react';

import { getFaultLevel, getFaultTag } from '@/services/datareport/rca';

const RcaModel = () => {
  const [faultLevels, setFaultLevels] = useState([]);
  const [faultTags, setFaultTags] = useState([]);

  async function fetchFaultLevel() {
    const { data } = await getFaultLevel();

    const levels = data?.fault_level_list.map(
      (v: { level_name: string; id: number }) => ({
        label: v.level_name,
        value: v.id,
      }),
    );

    setFaultLevels(levels);
    return levels;
  }

  async function fetchFaultTag() {
    const { data } = await getFaultTag();

    const tags = data?.tag_list.map((v: { tag_name: string; id: number }) => ({
      label: v.tag_name,
      value: v.id,
    }));

    setFaultTags(tags);
    return tags;
  }

  return {
    faultLevels,
    faultTags,
    fetchFaultLevel,
    fetchFaultTag,
  };
};

export default createModel(RcaModel);
